<template>
  <div class="register-container">
    <div class="auth-card">
      <h2 class="auth-title">美味餐厅注册</h2>
      <form class="auth-form" @submit.prevent="handleRegister">
        <!-- 用户名输入 -->
        <div class="input-group">
          <label class="input-label">用户名</label>
          <input
              v-model="form.username"
              type="text"
              placeholder="请输入4-16位用户名"
              required
              class="auth-input"
              minlength="4"
              maxlength="16"
          />
        </div>

        <!-- 密码输入 -->
        <div class="input-group">
          <label class="input-label">密码</label>
          <input
              v-model="form.password"
              type="password"
              placeholder="请输入6-18位密码"
              required
              class="auth-input"
              minlength="6"
              maxlength="18"
          />
        </div>

        <!-- 操作按钮 -->
        <div class="action-group">
          <button type="submit" class="submit-btn" :disabled="loading">
            <span v-if="!loading">立即注册</span>
            <div v-else class="loading-dots">
              <div class="dot"></div>
              <div class="dot"></div>
              <div class="dot"></div>
            </div>
          </button>
          <div class="login-link">
            已有账号？
            <a href="#" @click.prevent="$router.push('/login')">立即登录</a>
          </div>
        </div>
      </form>
    </div>
  </div>
  <!-- 在模板底部添加消息容器 -->
  <teleport to="body">
    <transition name="shake">
      <div v-if="message.show" :class="['alert-message', message.type]">
        <svg class="icon" viewBox="0 0 24 24">
          <path v-if="message.type === 'error'" fill="currentColor" d="M13,14H11V10H13M13,18H11V16H13M1,21H23L12,2L1,21Z"/>
          <path v-else fill="currentColor" d="M12 2C6.5 2 2 6.5 2 12S6.5 22 12 22 22 17.5 22 12 17.5 2 12 2M10 17L5 12L6.41 10.59L10 14.17L17.59 6.58L19 8L10 17Z"/>
        </svg>
        {{ message.text }}
      </div>
    </transition>
  </teleport>
</template>

<script setup>
import { ref } from 'vue'
import { userRegister } from '@/api/index.js'
import { useRouter } from 'vue-router'

const router = useRouter()
const form = ref({ username: '', password: '' })
const loading = ref(false)

const handleRegister = async () => {
  loading.value = true
  try {
    const res = await userRegister(form.value)
    
    if (res?.success) {
      showMessage('注册成功', 'success')
      setTimeout(() => router.push('/login'), 1500)
    } else {
      showMessage(res?.message || '注册失败，请检查输入', 'error')
    }
  } catch (error) {
    showMessage(error.response?.data?.message || '注册失败，请稍后重试', 'error')
  } finally {
    loading.value = false
  }
}

// const showMessage = (text, type) => {
//   const msg = document.createElement('div')
//   msg.className = `alert-message ${type}`
//   msg.textContent = text
//   document.body.appendChild(msg)
//   setTimeout(() => msg.remove(), 3000)
// }
const message = ref({
  show: false,
  text: '',
  type: 'info'
})

const showMessage = (text, type) => {
  message.value = { show: true, text, type }
  setTimeout(() => {
    message.value.show = false
  }, 4000)
}
</script>

<style scoped lang="scss">
.register-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.auth-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 420px;
  transition: transform 0.3s ease;

  &:hover {
    transform: translateY(-2px);
  }
}

.auth-title {
  text-align: center;
  color: #2d3748;
  font-size: 1.8rem;
  margin-bottom: 2rem;
  font-weight: 600;
  letter-spacing: 1px;
}

.input-group {
  margin-bottom: 1.5rem;
}

.input-label {
  display: block;
  color: #4a5568;
  font-size: 0.95rem;
  margin-bottom: 0.5rem;
  padding-left: 8px;
}

.auth-input {
  width: 100%;
  height: 48px;
  padding: 0 16px;
  border: 2px solid #e2e8f0;
  border-radius: 8px;
  font-size: 1rem;
  transition: all 0.3s ease;

  &:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.2);
  }

  &::placeholder {
    color: #a0aec0;
  }
}

.action-group {
  margin-top: 2rem;
}

.submit-btn {
  width: 100%;
  height: 48px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover:not(:disabled) {
    opacity: 0.9;
    transform: translateY(-1px);
  }

  &:disabled {
    opacity: 0.7;
    cursor: not-allowed;
  }
}

.loading-dots {
  display: flex;
  justify-content: center;
  gap: 6px;
  height: 24px;

  .dot {
    width: 8px;
    height: 8px;
    background: white;
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out;

    &:nth-child(2) {
      animation-delay: 0.2s;
    }

    &:nth-child(3) {
      animation-delay: 0.4s;
    }
  }
}

.login-link {
  text-align: center;
  margin-top: 1.5rem;
  color: #718096;
  font-size: 0.95rem;

  a {
    color: #667eea;
    text-decoration: none;
    font-weight: 500;

    &:hover {
      color: #764ba2;
    }
  }
}

.alert-message {
  position: fixed;
  top: 20px;
  right: 20px;
  padding: 12px 20px;
  border-radius: 8px;
  color: white;
  font-size: 0.9rem;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

  &.success {
    background: #48bb78;
  }

  &.error {
    background: #f56565;
  }
}

@keyframes bounce {
  0%, 80%, 100% {
    transform: translateY(0);
  }
  40% {
    transform: translateY(-6px);
  }
}

@media (max-width: 480px) {
  .auth-card {
    padding: 24px;
    border-radius: 12px;
  }

  .auth-title {
    font-size: 1.5rem;
    margin-bottom: 1.5rem;
  }

  .auth-input {
    height: 44px;
    font-size: 0.95rem;
  }

  .submit-btn {
    height: 44px;
    font-size: 0.95rem;
  }
}
.alert-message {
  position: fixed;
  top: 20px;
  right: 20px;
  min-width: 280px;
  padding: 16px 24px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 16px;
  font-weight: 500;
  box-shadow: 0 8px 24px rgba(0,0,0,0.15);
  z-index: 9999;

  .icon {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
  }

  &.error {
    background: #ff4444;
    color: white;
    border: 2px solid #ff0000;

    .icon {
      animation: flash 0.8s ease-in-out;
    }
  }

  &.success {
    background: #00C851;
    color: white;
    border: 2px solid #007E33;
  }
}

/* 震动动画 */
.shake-enter-active {
  animation: shake 0.5s cubic-bezier(.36,.07,.19,.97) both;
}

@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(3px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}

@keyframes flash {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.3; }
}

@media (max-width: 480px) {
  .alert-message {
    left: 20px;
    right: 20px;
    min-width: auto;
  }
}
</style>